//
// File: std.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 25-Apr-2025 12:42:26
//

// Include Files
#include "std.h"
#include "blockedSummation.h"
#include "rt_nonfinite.h"
#include "coder_array.h"
#include <cmath>
#include <string.h>

// Function Definitions
//
// Arguments    : const ::coder::array<double, 1U> &x
// Return Type  : double
//
namespace coder {
double b_std(const ::coder::array<double, 1U> &x)
{
  array<double, 1U> absdiff;
  double y;
  int kend;
  kend = x.size(0);
  if (x.size(0) == 0) {
    y = rtNaN;
  } else if (x.size(0) == 1) {
    if ((!std::isinf(x[0])) && (!std::isnan(x[0]))) {
      y = 0.0;
    } else {
      y = rtNaN;
    }
  } else {
    double xbar;
    xbar = blockedSummation(x, x.size(0)) / static_cast<double>(x.size(0));
    absdiff.set_size(x.size(0));
    for (int k{0}; k < kend; k++) {
      absdiff[k] = std::abs(x[k] - xbar);
    }
    y = 0.0;
    xbar = 3.3121686421112381E-170;
    kend = x.size(0);
    for (int k{0}; k < kend; k++) {
      if (absdiff[k] > xbar) {
        double t;
        t = xbar / absdiff[k];
        y = y * t * t + 1.0;
        xbar = absdiff[k];
      } else {
        double t;
        t = absdiff[k] / xbar;
        y += t * t;
      }
    }
    y = xbar * std::sqrt(y);
    y /= std::sqrt(static_cast<double>(x.size(0)) - 1.0);
  }
  return y;
}

} // namespace coder

//
// File trailer for std.cpp
//
// [EOF]
//
